package com.example.coursemanagement.controller;


import com.example.coursemanagement.MyUtils.CommonResult;
import com.example.coursemanagement.MyUtils.ResultCode;
import com.example.coursemanagement.mapper.CourseMapper;
import com.example.coursemanagement.mapper.StudentCourseMapper;
import com.example.coursemanagement.pojo.Course;
import com.example.coursemanagement.pojo.Curriculum;
import com.example.coursemanagement.pojo.StudentCourse;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cairunxing
 * @since 2023-11-29
 */

//没问题
@RestController
@RequestMapping("/course" )
@RequiredArgsConstructor
public class CourseController{

    private final CourseMapper courseMapper;
    private final StudentCourseMapper studentCourseMapper;

    @ApiOperation(value = "查询所有")
    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    public CommonResult<Object> queryAllCourse(){
        Collection<Course> courses = courseMapper.selectList(null);

        if(courses==null){
            return CommonResult.failed(ResultCode.FAILED);
        }
        else{
            return CommonResult.success(courses);
        }
    }



    @ApiOperation(value = "根据过滤条件学生id、第几周、学期查询课表")
    @RequestMapping(value = "/selectCurriculum",method = RequestMethod.GET)
    public CommonResult<Object> queryCurriculum(int studentId,int week,String term){

//        Map map = new HashMap<>();
//        map.put("student_id",studentId);
//
//        List<StudentCourse> StudentCourses = studentCourseMapper.selectByMap(map);

        List<StudentCourse> StudentCourses = studentCourseMapper.selectByStudentId(studentId);

        if(StudentCourses.isEmpty()){
            return CommonResult.failed(ResultCode.FAILED);
        }
        else{
            List<Curriculum> curriculums = new ArrayList<>();
            for(int num = 0;num < StudentCourses.size();num++)
            {
                curriculums.add(courseMapper.selectCurriculum(StudentCourses.get(num).getCourseId()));
            }

            List<Curriculum> collect = curriculums.stream().filter(curriculum -> {
                if(term.equals(curriculum.getSemester())){
                    return isWeekInRange(curriculum, week);
                }
                return false;
            }).collect(Collectors.toList());
            return CommonResult.success(collect);
        }
    }


    @ApiOperation(value = "根据课程id查询某个课程的详细信息")
    @RequestMapping(value = "/selectTheCourse",method = RequestMethod.GET)
    public CommonResult<Object> queryTheCourse(int courseId){
        Course course = courseMapper.selectCourseInformation(courseId);

        if(course==null){
            return CommonResult.failed(ResultCode.FAILED);
        }
        else{
            return CommonResult.success(course);
        }
    }

    @ApiOperation(value = "根据学生id和学期查看某一学期的课程成绩")
    @RequestMapping(value = "/selectGrade",method = RequestMethod.GET)
    public CommonResult<Object> queryCrade(int studentId,String term){

        List<StudentCourse> StudentCourses = studentCourseMapper.selectToGrade(studentId,term);

        if(StudentCourses.isEmpty()){
            return CommonResult.failed(ResultCode.FAILED);
        }
        else{

            return CommonResult.success(StudentCourses);
        }
    }




    @ApiOperation("新增课程")
    @RequestMapping(value = "/addCourse",method = RequestMethod.POST)
    public CommonResult<Object> addCourse(Course course){

        if(courseMapper.insert(course)>0){
            return CommonResult.success(ResultCode.ADD_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

    @ApiOperation("编辑操作")
    @RequestMapping(value = "/updateCourse",method = RequestMethod.POST)
    public CommonResult<Object> updateCourse(Course course){
        if(courseMapper.updateById(course)>0){
            return CommonResult.success(ResultCode.UPDATE_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

    @ApiOperation("删除操作")
    @RequestMapping(value = "/deleteCourse",method = RequestMethod.DELETE)
    public CommonResult<Object> deleteCourse(int id){
        if(courseMapper.deleteById(id)>0){
            return CommonResult.success(ResultCode.DELETE_SUCCESS);
        }
        else {
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

    private static boolean isWeekInRange(Curriculum curriculum, int targetWeek) {
        String[] weekRange = curriculum.getWeek().split(",");
        int startWeek = Integer.parseInt(weekRange[0]);
        int endWeek = Integer.parseInt(weekRange[1]);

        return targetWeek >= startWeek && targetWeek <= endWeek;
    }

}

